package com.tiger.angrybirds;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.util.Log;
import android.widget.Toast;

public class MusicPlayServic extends Service {
	private static final String TAG = "zm";
	MediaPlayer player;
	boolean destoryIt = false;
	Bundle bundle;
	int musicId = 0;

	@Override
	public void onCreate() {
//		Log.i(TAG, "onCreate");
//		Toast.makeText(this, "Music Service Created", Toast.LENGTH_LONG).show();

	}

	@Override
	public void onDestroy() {
//		Log.i(TAG, "onDestroy");
//		Toast.makeText(this, "Music Service Stopped", Toast.LENGTH_LONG).show();
		destoryIt = true;
		player.stop();
	}

	@Override
	public IBinder onBind(Intent intent) {
//		 Log.i(TAG, "onBind");
		IBinder result = null;
		if (null == result)
			result = new MyBinder();
		return result;

		
		// bundle = intent.getExtras();
		// musicId = bundle.getInt("musicId");
		// Log.i("zm", "onBind musicId===" + musicId);
		// return null;
	}

	@Override
	public void onStart(Intent intent, int startid) {
		super.onStart(intent, startid);
//		Log.i(TAG, "onStart");
		if(player!=null)
			player.stop();
		bundle = intent.getExtras();
		musicId = bundle.getInt("musicId");
		musicId = CommonUtil.rawIds[musicId];
//		Toast.makeText(this, "Music Service Started", Toast.LENGTH_LONG).show();
		// 设定，要播放的mp3文件
		player = MediaPlayer.create(this, musicId);
		player.setLooping(false); // Set looping
		player.setOnCompletionListener(new OnCompletionListener() {
			@Override
			public void onCompletion(MediaPlayer arg0) {
				// TODO Auto-generated method stub
				Toast
						.makeText(MusicPlayServic.this, "播放完成",
								Toast.LENGTH_SHORT).show();
			}
		});
		try {
			player.prepare();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//			File file = new File("/sdcard/birds/.temp");
//			
//			Log.i("zm", "musicId===" + musicId);
//			InputStream ins = this.getResources().openRawResource(musicId);
//			OutputStream os;
//
//			os = new FileOutputStream(file);
//
//			int bytesRead = 0;
//			byte[] buffer = new byte[8192];
//			while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
//				os.write(buffer, 0, bytesRead);
//			}
//			os.close();
//			ins.close();
//			player.setDataSource(this, Uri.fromFile(file));
		
		player.start();
	}

	public class MyBinder extends Binder {
		// 此方法是为了可以在Acitity中获得服务的实例
		public MusicPlayServic getService() {
			return MusicPlayServic.this;
		}

		// 这个方法主要是接收Activity发向服务的消息，data为发送消息时向服务传入的对象，replay是由服务返回的对象
		// public boolean onTransact(int code, Parcel data, Parcel reply, int
		// flags) {
		// // called when client calls transact on returned Binder
		// return handleTransactions(code, data, reply, flags);
		// }

	}
}
